﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Threading;
namespace DomainSearchTools.Common
{
    /// <summary>
    /// 域名搜索推入池
    /// </summary>
    public class DomainPushHelper
    {
        private string prefixBegin;
        /// <summary>
        /// 前缀开始
        /// </summary>
        public string PrefixBegin
        {
            get { return prefixBegin; }
            set { prefixBegin = value; }
        }
        private string prefixEnd;
        /// <summary>
        /// 前缀结束
        /// </summary>
        public string PrefixEnd
        {
            get { return prefixEnd; }
            set { prefixEnd = value; }
        }

        private string[] suffix;
        /// <summary>
        /// 后缀
        /// </summary>
        public string[] Suffix
        {
            get { return suffix; }
            set { suffix = value; }
        }

        private SearchMode mode;
        /// <summary>
        /// 搜索模式
        /// </summary>
        public SearchMode Mode
        {
            get { return mode; }
            set { mode = value; }
        }

        private int pushSpeed = 100;
        /// <summary>
        /// 推入速度(默认100毫秒)
        /// </summary>
        public int PushSpeed
        {
            get { return pushSpeed; }
            set { pushSpeed = value; }
        }


        private PushStatus status ;
        /// <summary>
        /// 推入状态
        /// </summary>
        public PushStatus Status
        {
            get { return status; }
            set { status = value; }
        }
        public DomainPushHelper(string prefixBegin, SearchMode mode, string prefixEnd, string[] suffix)
        {
            this.prefixBegin = prefixBegin;
            this.mode = mode;
            this.prefixEnd = prefixEnd;
            this.suffix = suffix;
        }

        Thread threadPush = null;

        /// <summary>
        /// 开始推入数据
        /// </summary>
        public void StartPush()
        {
            threadPush = new Thread(new ThreadStart(DoPush));
            threadPush.Start();
        }
        /// <summary>
        /// 停止推入数据
        /// </summary>
        public void StopPush()
        {
            threadPush.Abort();
        }

        /// <summary>
        /// 获取域名列表
        /// </summary>
        /// <param name="mode"></param>
        /// <returns></returns>
        private void DoPush()
        {
            status = PushStatus.PUSHING;
            switch (mode)
            {
                case SearchMode.A2Z1:
                    var t = Config.a2z;
                    for (var i = 0; i < t.Length; i++)
                    {
                        foreach (string suffix in this.suffix)
                        {
                            DomainPoolHelper.List.Add(new DomainPoolModel()
                            {
                                DomainName = this.prefixBegin + t.Substring(i, 1) + this.prefixEnd + suffix,
                                //Suffix = suffix,
                                IsSearched = false,
                                Status = DomainStatus.UNKNOWN
                            });
                        }

                        Thread.Sleep(pushSpeed);
                    }
                    break;
                case SearchMode.A2Z2:
                    t = Config.a2z;
                    for (var i = 0; i < t.Length; i++)
                    {
                        for (var n = 0; n < t.Length; n++)
                        {
                            foreach (string suffix in this.suffix)
                            {
                                DomainPoolHelper.List.Add(new DomainPoolModel()
                                      {
                                          DomainName = this.prefixBegin + t.Substring(i, 1) + t.Substring(n, 1) + this.prefixEnd + suffix,
                                          //Suffix = suffix,
                                          IsSearched = false,
                                          Status = DomainStatus.UNKNOWN
                                      });
                            }
                            Thread.Sleep(pushSpeed);
                        }
                    }
                    break;
                case SearchMode.A2Z3:
                    t = Config.a2z;
                    for (var i = 0; i < t.Length; i++)
                    {
                        for (var n = 0; n < t.Length; n++)
                        {
                            for (var k = 0; k < t.Length; k++)
                            {
                                foreach (string suffix in this.suffix)
                                {
                                    DomainPoolHelper.List.Add(new DomainPoolModel()
                                       {
                                           DomainName = this.prefixBegin + t.Substring(i, 1) + t.Substring(n, 1) + t.Substring(k, 1) + this.prefixEnd + suffix,
                                           //Suffix = suffix,
                                           IsSearched = false,
                                           Status = DomainStatus.UNKNOWN
                                       });
                                }
                                Thread.Sleep(pushSpeed);
                            }
                        }
                    }
                    break;
                case SearchMode.PINGYIN:
                    var p = Config.pingyin.Split(',');
                    foreach (string item in p)
                    {
                        foreach (string suffix in this.suffix)
                        {
                            DomainPoolHelper.List.Add(new DomainPoolModel()
                            {
                                DomainName = this.prefixBegin + item + this.prefixEnd + suffix,
                                //Suffix = suffix,
                                IsSearched = false,
                                Status = DomainStatus.UNKNOWN
                            });
                        }
                        Thread.Sleep(pushSpeed);
                    }
                    break;
            }

            status = PushStatus.FINISHED;
        }
    }
}
